﻿namespace Smart.Utils.Net
{
    using System;
    using System.IO;
    using System.Text;

    public class MailEncoder
    {
        public static string ConvertToQP(string s, string charset)
        {
            if (s == null)
            {
                return null;
            }
            if (charset == null)
            {
                charset = "ISO-8859-1";
            }
            char[] chArray = Environment.NewLine.ToCharArray();
            char[] chArray2 = s.ToCharArray();
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            bool flag = false;
            Encoding encoding = Encoding.GetEncoding(charset);
            for (int i = 0; i < s.Length; i++)
            {
                char ch = chArray2[i];
                if ((ch == chArray[0]) && (i <= (s.Length - chArray.Length)))
                {
                    if (s.Substring(i, chArray.Length) == Environment.NewLine)
                    {
                        if (builder2.Length > 0)
                        {
                            if (chArray2[i - 1] == ' ')
                            {
                                builder2.Remove(builder2.Length - 1, 1);
                                builder2.Append("=20");
                            }
                            else if (chArray2[i - 1] == '\t')
                            {
                                builder2.Remove(builder2.Length - 1, 1);
                                builder2.Append("=09");
                            }
                        }
                        flag = true;
                        i += chArray.Length - 1;
                    }
                    else
                    {
                        builder2.Append("=0" + Convert.ToString((byte) ch, 0x10).ToUpper());
                    }
                }
                else if (((ch > '~') || ((ch < ' ') && (ch != '\t'))) || (ch == '='))
                {
                    byte[] bytes = new byte[10];
                    int num2 = encoding.GetBytes(ch.ToString(), 0, 1, bytes, 0);
                    for (int j = 0; j < num2; j++)
                    {
                        if (bytes[j] < 0x10)
                        {
                            builder2.Append("=0" + Convert.ToString(bytes[j], 0x10).ToUpper());
                        }
                        else
                        {
                            builder2.Append("=" + Convert.ToString(bytes[j], 0x10).ToUpper());
                        }
                    }
                }
                else
                {
                    builder2.Append(ch);
                }
                if (builder2.Length >= 0x4c)
                {
                    string str = builder2.ToString();
                    int length = str.LastIndexOf("=");
                    if (length < 70)
                    {
                        length = 0x4a;
                    }
                    builder.Append(str.Substring(0, length) + "=\r\n");
                    builder2 = new StringBuilder(str.Substring(length));
                }
                if (flag)
                {
                    builder.Append(builder2.ToString() + "\r\n");
                    builder2 = new StringBuilder();
                    flag = false;
                }
            }
            builder.Append(builder2.ToString());
            return builder.ToString();
        }

        public static string GetBase64String(string filePath)
        {
            string str = "";
            if (File.Exists(filePath))
            {
                FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[stream.Length];
                int length = stream.Read(buffer, 0, buffer.Length);
                str = Convert.ToBase64String(buffer, 0, length);
                stream.Close();
                StringBuilder builder = new StringBuilder();
                int num2 = 0x4c;
                for (int i = 0; i < str.Length; i += num2)
                {
                    if ((str.Length - (i + num2)) < 0)
                    {
                        num2 = str.Length - i;
                    }
                    builder.Append(str.Substring(i, num2));
                    builder.Append("\r\n");
                }
                return builder.ToString();
            }
            return null;
        }
    }
}

